package by.jabbby.dataprovider;


import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import by.jabbby.Logger;

public class Dictionary extends SQLiteOpenHelper {
	
	public static final String TABLE_MESSAGE_HISTORY = "message_history";
	public static final String T_MH_ID = "id";
	public static final String T_MH_TO = "msg_to"; // id contact
	public static final String T_MH_FROM = "msg_from";// id contact
	public static final String T_MH_MESSAGE = "message";
	public static final String T_MH_DATE = "date";
	public static final String T_MH_ChatType = "chat_type";
	
	public static final String TABLE_CONTACT = "contact";
	public static final String T_CONT_ID = "id";
	public static final String T_CONT_JID = "jid";
	
	
	
	//public static final String TABLE_MESSAGE_HISTORY = "message_history";
	//public static final String TABLE_MESSAGE_HISTORY = "message_history";

	private static final String DB_NAME = "by_jabbby";
	private static final int DB_VERSION= 7;
	private static final String initional_T_MESSAGE_HISTORY_SQL = "CREATE TABLE " + TABLE_MESSAGE_HISTORY + " ("
    + T_MH_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
    + T_MH_TO + " INT,"
    + T_MH_FROM + " INT,"
    + T_MH_MESSAGE + " TEXT,"
    + T_MH_DATE + " INT,"
    + T_MH_ChatType +" INT)";
	
	private static final String initional_T_CONTACT = "CREATE TABLE " + TABLE_CONTACT + " ("
    
	+ T_CONT_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
    + T_CONT_JID + " TEXT)";
    
	private static final String drop_T_CONTACT = "DROP TABLE  IF EXISTS " + TABLE_CONTACT;
	private static final String drop_T_MESSAGE_HISTORY_SQL = "DROP TABLE IF EXISTS " + TABLE_MESSAGE_HISTORY;
		
	public Dictionary(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
    	Logger.i("Create db table");
    	try{
        db.execSQL(initional_T_CONTACT);
        db.execSQL(initional_T_MESSAGE_HISTORY_SQL);
    	}catch (Exception e) {
			Logger.e("Exeption when create db table", e);
		}
    }

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion,
			int newVersion) {
		try{
		if(oldVersion != newVersion){
			db.execSQL( drop_T_MESSAGE_HISTORY_SQL);
			db.execSQL( drop_T_CONTACT);
			this.onCreate(db);
		}
		// TODO Auto-generated method stub
		}catch (Exception e) {
			Logger.e("Error when update db", e);
		}
	}
	
	
}
